package proeva;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;

public class Report_DDoS_H_Servlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Report_DDoS_H_Servlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String path = request.getSession().getServletContext().getRealPath(".");
		FileWriter fw = new FileWriter( path + "\\test.html");
		BufferedWriter bw = new BufferedWriter( fw );
		//udp
		if( request.getParameter("UorS").equalsIgnoreCase("u") )
		{
			//写html内容
			bw.write("<html><head></head><body><center><H1>UDP flood 攻击防守绩效报告</H1>");
			try
			{
				Connection Conn = DBSetting.GetEvaConnection();
				Statement stmt = Conn.createStatement();
				ResultSet result = stmt.executeQuery("SELECT * FROM DDoS_Eva_H WHERE Type='true' ORDER BY Eva_Time DESC");
				while( result.next() )
				{	
					Timestamp Eva_Time = result.getTimestamp("Eva_Time");				
					double Bitrate = result.getDouble("Bitrate");				
					double RequestRate = result.getDouble("RequestRate");				
					double RequestSize = result.getDouble("RequestSize");
					double FileSize = result.getDouble("FileSize");
					double Eva = result.getDouble("Eva");		

					bw.write("<table border = 1 width=350><caption>评估时间："+Eva_Time+"</caption>");
					bw.write("<tr><td width=150>瓶颈链路带宽：</td><td>"+Bitrate+"</td></tr>");
					bw.write("<tr><td width=150>请求率：</td><td>"+RequestRate+"</td></tr>");
					bw.write("<tr><td width=150>请求数据包大小：</td><td>"+RequestSize+"</td></tr>");
					bw.write("<tr><td width=150>请求数据长度：</td><td>"+FileSize+"</td></tr>");
					bw.write("<tr><td width=150>防守绩效：</td><td>"+Eva+"</td></tr>");
					bw.write("</table>");
					bw.write("<br/>");
				}
				result.close();
				stmt.close();
				Conn.close();
			}
			catch( Exception ex )
			{
				response.getWriter().write("读取数据库发生错误，请重试");
				return;
			}


			bw.write("</center></body></html>");
			bw.flush();
			bw.close();
			fw.close();

			//下面是进行下载生成的html的工作
			response.reset();//可以加也可以不加
			response.setContentType("application/x-download");

			String filedownload = path + "\\test.html";
			String filedisplay = "Report.html";
			filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
			response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

			java.io.OutputStream outp = null;
			java.io.FileInputStream in = null;
			try
			{
				outp = response.getOutputStream();
				in = new FileInputStream(filedownload);

				byte[] b = new byte[1024];
				int i = 0;

				while((i = in.read(b)) > 0)
				{
					outp.write(b, 0, i);
				}
				//  
				outp.flush();
			}
			catch(Exception e)
			{
				System.out.println("Error!");
				e.printStackTrace();
			}
			finally
			{
				if(in != null)
				{
					in.close();
					in = null;
				}
			}
		}
		// syn
		else
		{
			//写html内容
			bw.write("<html><head></head><body><center><H1>SYN flood 攻击防守绩效报告</H1>");
			try
			{
				Connection Conn = DBSetting.GetEvaConnection();
				Statement stmt = Conn.createStatement();
				ResultSet result = stmt.executeQuery("SELECT * FROM DDoS_Eva_H WHERE Type='false' ORDER BY Eva_Time DESC");
				while( result.next() )
				{	
					Timestamp Eva_Time = result.getTimestamp("Eva_Time");				
					double Bitrate = result.getDouble("Bitrate");				
					double RequestRate = result.getDouble("RequestRate");				
					double RequestSize = result.getDouble("RequestSize");
					double FileSize = result.getDouble("FileSize");
					double Qlimit = result.getDouble("Qlimit");
					double Eva = result.getDouble("Eva");		

					bw.write("<table border = 1 width=350><caption>评估时间："+Eva_Time+"</caption>");
					bw.write("<tr><td width=150>瓶颈链路带宽：</td><td>"+Bitrate+"</td></tr>");
					bw.write("<tr><td width=150>请求率：</td><td>"+RequestRate+"</td></tr>");
					bw.write("<tr><td width=150>请求数据包大小：</td><td>"+RequestSize+"</td></tr>");
					bw.write("<tr><td width=150>请求数据长度：</td><td>"+FileSize+"</td></tr>");
					bw.write("<tr><td width=150>半开连接队列长度：</td><td>"+Qlimit+"</td></tr>");
					bw.write("<tr><td width=150>防守绩效：</td><td>"+Eva+"</td></tr>");
					bw.write("</table>");
					bw.write("<br/>");
				}
				result.close();
				stmt.close();
				Conn.close();
			}
			catch( Exception ex )
			{
				response.getWriter().write("读取数据库发生错误，请重试");
				return;
			}


			bw.write("</center></body></html>");
			bw.flush();
			bw.close();
			fw.close();

			//下面是进行下载生成的html的工作
			response.reset();//可以加也可以不加
			response.setContentType("application/x-download");

			String filedownload = path + "\\test.html";
			String filedisplay = "Report.html";
			filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
			response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

			java.io.OutputStream outp = null;
			java.io.FileInputStream in = null;
			try
			{
				outp = response.getOutputStream();
				in = new FileInputStream(filedownload);

				byte[] b = new byte[1024];
				int i = 0;

				while((i = in.read(b)) > 0)
				{
					outp.write(b, 0, i);
				}
				//  
				outp.flush();
			}
			catch(Exception e)
			{
				System.out.println("Error!");
				e.printStackTrace();
			}
			finally
			{
				if(in != null)
				{
					in.close();
					in = null;
				}
			}
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet( request, response );
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
